package com.softeem.hbsm_video.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.hbsm_video.entity.Admin;
import com.softeem.hbsm_video.service.AdminService;
import com.softeem.hbsm_video.entity.Result;
import com.softeem.hbsm_video.util.JwtUtil;
import com.softeem.hbsm_video.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@RequestMapping(value = "/admin")
@RestController
public class AdmainController {
    @Autowired
    private AdminService adminService;
    private String string;

    @GetMapping(value = "add")
    public boolean add(Admin admin) {
        boolean b = adminService.exists(Wrappers.query(Admin.class).eq("username", admin.getUsername()));
        if (b) {
            return false;
        }
        //生成随机盐
        String salt = RandomUtil.randomString(16);

        String pwd = admin.getPassword() + salt;

        pwd = SecureUtil.md5(pwd);

        admin.setPassword(pwd);
        admin.setSalt(salt);

        return adminService.save(admin);
    }


    @GetMapping(value = "/login")
    @PostMapping(value = "/login")
        public Result<String> login (String username, String password){
            // 1.
            Admin admin = adminService.getOne(Wrappers.query(Admin.class).eq("username", username));
            if (admin == null) {
                return Result.error("用户名不存在");
            } else {
                String salt = admin.getSalt();
                String pwd = SecureUtil.md5(password + salt);
                if (pwd.equals(admin.getPassword())) {
                    // 生成token
                    Map<String, Object> claims = new HashMap<>();
                    claims.put("id", admin.getId());
                    claims.put("name", admin.getUsername());
                    String token = JwtUtil.getToken(claims);
                    return Result.success(token);
                } else {
                    return Result.error("密码错误");
                }
            }
        }

        @PostMapping("/updateAdmin")
        public Result<String> updateUser (Long id, String newPassword){
            Admin admin = adminService.getById(id);
            if (admin == null) {
                return Result.error("用户不存在");
            }
            String salt = admin.getSalt();
            String pwd = SecureUtil.md5(newPassword + salt);
            admin.setPassword(pwd);
            adminService.updateById(admin);
            return Result.success("用户信息更新成功");
        }

        @PostMapping("/deleteAdmin")
        public Result<String> deleteUser (Long id){
            boolean success = adminService.removeById(id);
            if (success) {
                return Result.success("用户删除成功");
            } else {
                return Result.error("用户删除失败");
            }
        }

        @GetMapping("/infoAdmin") //获取当前登录管理员信息
        public Result<Admin> info () {
            Map<String, Object> map = ThreadLocalUtil.get();
            Admin admin = adminService.getById((Integer) map.get("id"));
            if (admin == null) {
                return Result.error("获取失败");
            }
            return Result.success(admin);
        }

}
